package com.untilov.gb.xml.listener;

import java.util.Hashtable;
import java.util.Vector;

import com.alsutton.xmlparser.XMLEventListener;
import com.untilov.gb.model.FeedItem;
import com.untilov.gb.xml.listener.util.TagsNames;

/**
 * SAX listener, contain callback methods from parser.
 * 
 * @author iuntilov
 * 
 */
public class GBookmarksSAXListener implements XMLEventListener {

    private Vector feeds;

    private FeedItem currentFeed;

    private String currentTag;

    public GBookmarksSAXListener(Vector argFeeds) {
        this.feeds = argFeeds;
    }

    public void plaintextEncountered(String text) {
        if (this.currentTag != null) {
            if (this.currentTag.equals(TagsNames.TITLE)) {
                this.currentFeed.setTitle(text.trim());
            } else if (this.currentTag.equals(TagsNames.URL)) {
                this.currentFeed.setUrl(text.trim());
            } else if (this.currentTag.equals(TagsNames.LABEL)) {
                this.currentFeed.setCategory(text.trim());
            }
            // else if (this.currentTag.equals(TagsNames.DESCRIPTION)) {
            // this.currentFeed.setDetails(text.trim());
            // }
        }
    }

    public void tagEnded(String tagName) {
        if (tagName.equals(TagsNames.BOOKMARK)) {
            this.feeds.addElement(this.currentFeed);
        }
        this.currentTag = null;
    }

    public void tagStarted(String tagName, Hashtable attrs) {
        if (!tagName.equals(TagsNames.BOOKMARK) && this.currentFeed == null) {
            return;
        }
        if (tagName.equals(TagsNames.BOOKMARK)) {
            this.currentFeed = new FeedItem();
            this.currentTag = TagsNames.BOOKMARK;
        } else if (tagName.equals(TagsNames.TITLE)) {
            this.currentTag = TagsNames.TITLE;
        } else if (tagName.equals(TagsNames.URL)) {
            this.currentTag = TagsNames.URL;
        } else if (tagName.equals(TagsNames.LABEL)) {
            this.currentTag = TagsNames.LABEL;
        }
        // else if (tagName.equals(TagsNames.DESCRIPTION)) {
        // this.currentTag = TagsNames.DESCRIPTION;
        // }
    }
}